package com.chatviewer.blog.base;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @description: 通用服务响应对象
 **/
//保证序列化json的时候,如果是null的对象,key也会消失
@Data
@NoArgsConstructor
public class CloudResult<T> implements Serializable {
    //1.定义属性
    private int status; //200

    private String msg; // 成功

    private T data;   // json格式的数据......



    //2.定义构造函数
    private CloudResult(int status) {
        this.status = status;
    }

    // status=200 ，data {"name":"张三"，“poststation”:"test"}
    private CloudResult(int status, T data) {
        this.status = status;
        this.data = data;
    }

    private CloudResult(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    // status=200  msg="成功"
    private CloudResult(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }


//
//    @JsonIgnore
//    //使之不在json序列化结果当中
//    //4.判断这个响应是不是一个正确的响应
//    public boolean isSuccess() {
//        return this.status == ResponseCode.SUCCESS.getCode();
//    }

    //5.定义返回对象的方法
    public static <T> CloudResult<T> createBySuccessMessage() {
        return new CloudResult<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> CloudResult<T> createBySuccessMessageMessage(String msg) {
        return new CloudResult<T>(ResponseCode.SUCCESS.getCode(), msg);
    }

    public static <T> CloudResult<T> createBySuccessMessage(T data) {
        return new CloudResult<T>(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getDesc(), data);
    }

    public static <T> CloudResult<T> createByCodeSuccess(int status, String msg, T data) {
        return new CloudResult<T>(status,msg, data);
    }

    public static <T> CloudResult<T> createBySuccessMessage(String msg, T data) {
        return new CloudResult<T>(ResponseCode.SUCCESS.getCode(), msg, data);
    }

    public static <T> CloudResult<T> createByError() {
        return new CloudResult<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
    }

    public static <T> CloudResult<T> createByErrorMessage(String errorMessage) {
        return new CloudResult<T>(ResponseCode.ERROR.getCode(), errorMessage);
    }

    public static <T> CloudResult<T> createByErrorCodeMessage(int errorCode, String errorMessage) {
        return new CloudResult<T>(errorCode, errorMessage);
    }


    public static boolean isNullOrEmpty(String str) {
        return null == str || "".equals(str) || "null".equals(str);
    }

    public static boolean isNullOrEmpty(Object obj) {
        return null == obj || "".equals(obj);
    }

}